import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

/**
 * Part of the wizard-mode steps. Prompts the user as
 * to whether they want a code index included in their output.
 * @author James Stanier
 */
public class CodeIndexGUI implements ActionListener {
	
	VariableGUI wiz4;
	
	String outputFileName;
	String outputDir;
	String paperType;
	String variableOutput;
	int fontSize;
	boolean twoSidedPrinting;
	boolean inDepthSection;
	boolean keywordIndex;
	boolean codeIndex;
	
	JFrame window;
	JFrame prev;
	JButton nextButton;
	JButton backButton;
	JCheckBox codeIndexButton;
	
	ArrayList<File> inputFiles;
	
	/**
	 * Constructor. Takes the previous step as input.
	 * @param g Previous wizard step: VariableGUI.
	 */
	public CodeIndexGUI(VariableGUI g) {
		wiz4 = g;
		inputFiles = wiz4.getInputFiles();
		prev = wiz4.getWindow();
		outputFileName = wiz4.getOutputFileName();
		outputDir = wiz4.getOutputDir();
		paperType = wiz4.getPaperType();
		fontSize = wiz4.getFontSize();
		twoSidedPrinting = wiz4.isTwoSidedPrinting();
		inDepthSection = wiz4.isInDepthSection();
		keywordIndex = wiz4.isKeywordIndex();
		variableOutput = wiz4.getVariableOutput();
		codeIndex = false;
		codeIndexWindow();
	}
	
	/**
	 * Creates and draws the window.
	 */
	public void codeIndexWindow() {
		window = new JFrame("JavaLiN - Wizard mode");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(350,500);
		window.setLocationRelativeTo(null);
		window.toFront();
		window.setResizable(false);
		
		// Introductory passage and logo
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		top.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		// Logo
		JLabel logo = new JLabel();
		logo.setIcon(new ImageIcon("logo.png"));
		logo.setHorizontalAlignment(JLabel.CENTER);
		top.add(logo, BorderLayout.NORTH);
		
		// Heading information
		JPanel progress = new JPanel();
		JPanel header = new JPanel();
		progress.setLayout(new BorderLayout());
		header.setLayout(new BorderLayout());
		JLabel backwards = new JLabel("<HTML><font size=2>&#171; Variable output</HTML>");
		progress.add(backwards, BorderLayout.WEST);
		JLabel forwards = new JLabel("<HTML><font size=2> Title and author &#187;</HTML>");
		progress.add(forwards, BorderLayout.EAST);
		JLabel heading = new JLabel("<HTML><b><font size=5>5. Code index</b></HTML>");
		heading.setHorizontalAlignment(JLabel.CENTER);
		heading.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		header.add(heading, BorderLayout.CENTER);
		JLabel description = new JLabel("<HTML><font size=3>If you select the option below, " +
				"all input source files will be syntax highlighted in colour and place at the end " +
				"of the report.</HTML>");
		description.setBorder(BorderFactory.createEmptyBorder(20,10,10,10));
		header.add(description, BorderLayout.SOUTH);
		top.add(progress, BorderLayout.CENTER);
		top.add(header, BorderLayout.SOUTH);
		
		// Check box
		JPanel codeIndexPanel = new JPanel();
		codeIndexPanel.setBorder(BorderFactory.createEmptyBorder(10,0,0,10));
		codeIndexButton = new JCheckBox("Code index");
		codeIndexButton.addActionListener(this);
		codeIndexButton.setToolTipText("Create a syntax highlighted code index.");
		codeIndexPanel.add(codeIndexButton);
		
		// Navigation buttons
		JPanel navButtons = new JPanel();
		navButtons.setLayout(new BorderLayout());
		navButtons.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		backButton = new JButton("<< Back");
		backButton.addActionListener(this);
		navButtons.add(backButton, BorderLayout.WEST);
		nextButton = new JButton("Next >>");
		nextButton.addActionListener(this);
		navButtons.add(nextButton, BorderLayout.EAST);
		
		window.add("North",top);
		window.add("Center",codeIndexPanel);
		window.add("South",navButtons);
		window.validate();
		window.setVisible(true);
	}

	/**
	 * Describes actions to be taken upon component
	 * interaction.
	 */
	public void actionPerformed(ActionEvent e) {
		Object x = e.getSource();
		if(x.equals(codeIndexButton)) {
			codeIndex = codeIndexButton.isSelected();
		}
		else if(x.equals(backButton)) {
			clearSettings();
			window.setVisible(false);
			prev.setVisible(true);
		}
		else if(x.equals(nextButton)) {
			window.setVisible(false);
			WizTitleGUI wgui = new WizTitleGUI(this);
		}
	}
	
	/**
	 * Clear the settings of this screen.
	 */
	public void clearSettings() {
		codeIndex = false;
	}
	
	/**
	 * Get the current font size.
	 * @return The font size as an integer.
	 */
	public int getFontSize() {
		return fontSize;
	}
	
	/**
	 * Get the current paper type.
	 * @return The paper type as a String.
	 */
	public String getPaperType() {
		return paperType;
	}
	
	/**
	 * Get current two-sided printing status.
	 * @return Whether two-sided printing is enabled.
	 */
	public boolean isTwoSidedPrinting() {
		return twoSidedPrinting;
	}
	
	/**
	 * Get the current window.
	 * @return The current JFrame window.
	 */
	public JFrame getWindow() {
		return window;
	}
	
	/**
	 * Get the current output directory.
	 * @return The output directory as a String.
	 */
	public String getOutputDir() {
		return outputDir;
	}
	
	/**
	 * Get the output file name.
	 * @return The output file name as a String.
	 */
	public String getOutputFileName() {
		return outputFileName;
	}
	
	/**
	 * Get whether an in-depth section has been requested.
	 * @return Whether there will be an in-depth section as a boolean.
	 */
	public boolean isInDepthSection() {
		return inDepthSection;
	}
	
	/**
	 * Get whether a keyword index has been requested.
	 * @return Whether there will be a keyword index as a boolean.
	 */
	public boolean isKeywordIndex() {
		return keywordIndex;
	}
	
	/**
	 * Get the type of variable output requested.
	 * @return Variable output status as a String.
	 */
	public String getVariableOutput() {
		return variableOutput;
	}
	
	/**
	 * Get whether a code index has been requested.
	 * @return Whether there will be a code index as a boolean.
	 */
	public boolean isCodeIndex() {
		return codeIndex;
	}
	
	/**
	 * Get the input source file references.
	 * @return Input source files as File objects.
	 */
	public ArrayList<File> getInputFiles() {
		return inputFiles;
	}
}
